/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	TopMenuItemCmpst.java
 * 模块说明：	
 * 修改历史：
 * Dec 19, 2007 - lxm - 创建。
 */
package com.hd123.rumba.gwt.entry.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.Widget;
import com.hd123.rumba.gwt.entry.client.menu.QMenuItem;

/**
 * 顶级菜单项 | 界面部件
 * 
 * @author lxm
 * 
 */
class TopMenuItemCmpst extends Composite {

  public TopMenuItemCmpst(TopMenuCmpst owner) {
    super();
    assert owner != null;
    this.owner = owner;
    drawSelf();
  }

  private TopMenuCmpst owner;
  private QMenuItem menuItem;
  private boolean selected;

  public QMenuItem getMenuItem() {
    return menuItem;
  }

  public void setMenuItem(QMenuItem menuItem) {
    this.menuItem = menuItem;
    refresh();
  }

  public boolean getSelected() {
    return selected;
  }

  public void setSelected(boolean selected) {
    if (this.selected == selected)
      return;
    this.selected = selected;
    showSelected();
  }

  private class MenuItemHTML extends HTML {
    public void onBrowserEvent(Event event) {
      switch (DOM.eventGetType(event)) {
      case Event.ONMOUSEOVER:
        pnl_mouseOver();
        break;
      case Event.ONMOUSEOUT:
        pnl_mouseOut();
        break;
      case Event.ONCLICK:
        pnl_click();
        break;
      }
      super.onBrowserEvent(event);
    }
  }

  private class MenuItemImage extends Image {
    public void onBrowserEvent(Event event) {
      switch (DOM.eventGetType(event)) {
      case Event.ONMOUSEOVER:
        pnl_mouseOver();
        break;
      case Event.ONMOUSEOUT:
        pnl_mouseOut();
        break;
      case Event.ONCLICK:
        pnl_click();
        break;
      }
      super.onBrowserEvent(event);
    }
  }

  /** 布局框架 */
  private HorizontalPanel pnl;
  /** 左边 */
  private MenuItemHTML htmlLeft;
  /** 菜单图标 */
  private MenuItemImage imgIcon;
  /** 菜单标题 */
  private MenuItemHTML htmlCaption;
  /** 右边 */
  private MenuItemHTML htmlRight;

  private void drawSelf() {
    pnl = new HorizontalPanel();

    htmlLeft = new MenuItemHTML();
    htmlLeft.setStyleName("rb-topMenuItem-left");
    pnl.add(htmlLeft);

    imgIcon = new MenuItemImage();
    imgIcon.setVisible(false);
    imgIcon.addLoadListener(new LoadListener() {
      public void onError(Widget arg0) {
        imgIcon.setVisible(false);
      }

      public void onLoad(Widget arg0) {
        imgIcon.setVisible(true);
      }
    });
    pnl.add(imgIcon);

    htmlCaption = new MenuItemHTML();
    htmlCaption.setStyleName("rb-topMenuItem");
    pnl.add(htmlCaption);
    pnl.setCellVerticalAlignment(htmlCaption, HorizontalPanel.ALIGN_MIDDLE);

    htmlRight = new MenuItemHTML();
    htmlRight.setStyleName("rb-topMenuItem-right");
    pnl.add(htmlRight);

    this.initWidget(pnl);
  }

  private void pnl_mouseOver() {
    if (this.selected)
      return;
    htmlLeft.setStyleName("rb-topMenuItem-left-hot");
    imgIcon.setUrl(menuItem.getHotImage());
    htmlCaption.setStyleName("rb-topMenuItem-hot");
    htmlRight.setStyleName("rb-topMenuItem-right-hot");
  }

  private void pnl_mouseOut() {
    if (this.selected)
      return;
    htmlLeft.setStyleName("rb-topMenuItem-left");
    imgIcon.setUrl(menuItem.getImage());
    htmlCaption.setStyleName("rb-topMenuItem");
    htmlRight.setStyleName("rb-topMenuItem-right");
  }

  private void pnl_click() {
    // if (this.selected)
    // return;
    assert owner != null;
    MainFrame ep = this.owner.getEntryPoint();
    assert ep != null;
    ep.clickTopMenuItem(menuItem);
  }

  private void refresh() {
    if (menuItem.getImage() != null)
      imgIcon.setUrl(menuItem.getImage());
    htmlCaption.setText(menuItem.getCaption());
  }

  private void showSelected() {
    if (this.selected) {
      htmlLeft.setStyleName("rb-topMenuItem-left-selected");
      htmlCaption.setStyleName("rb-topMenuItem-selected");
      htmlRight.setStyleName("rb-topMenuItem-right-selected");
    } else {
      htmlLeft.setStyleName("rb-topMenuItem-left");
      htmlCaption.setStyleName("rb-topMenuItem-selected");
      htmlRight.setStyleName("rb-topMenuItem-right");
    }
  }
}
